package com.ruoyi.student.controller;

import com.ruoyi.common.core.utils.StringUtils;
import io.swagger.annotations.ApiParam;
import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.student.domain.StudentYfyd;
import com.ruoyi.student.service.IStudentYfydService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 学生一分一段Controller
 *
 * @author zyf
 * @date 2024-09-03
 */
@RestController
@RequestMapping("/yfyd")
public class StudentYfydController extends BaseController {
    @Autowired
    private IStudentYfydService studentYfydService;

    /**
     * 查询学生一分一段列表
     */
    @RequiresPermissions("student:yfyd:list")
    @GetMapping("/list")
    public TableDataInfo list(StudentYfyd studentYfyd) {
        studentYfyd.setScoreList(setSizes(studentYfyd.getScore()));
        studentYfyd.setPlaceList(setSizes(studentYfyd.getPlace()));

//        根据查询条件进行不同的查询方式优化性能
        if ((studentYfyd.getScoreList() != null && studentYfyd.getScoreList().size() != 0) ||
                (studentYfyd.getPlaceList() != null && studentYfyd.getPlaceList().size() != 0)) {
            //数据提前查出防止分页组件自动分页
            List<StudentYfyd> noPageList = studentYfydService.selectStudentYfydListNoPage(studentYfyd);
            startPage();
            List<StudentYfyd> list = studentYfydService.selectStudentYfydLists(studentYfyd, noPageList);
            return getDataTable(list);
        } else {
            startPage();
            List<StudentYfyd> list = studentYfydService.selectStudentYfydLists(studentYfyd);
            return getDataTable(list);
        }

    }

    //        设置查询范围
    public List<String> setSizes(String key) {
        ArrayList<String> list = new ArrayList<>();
        if (StringUtils.isNotEmpty(key)) {
            int place = Integer.parseInt(key);
            for (int i = 0; i <= 60; i++) {
                int x = place - i;
                if (x == 0) {
                    break;
                }
                list.add(place - i + "");
            }
        }
        return list;
    }

    /**
     * 导出学生一分一段列表
     */
    @RequiresPermissions("student:yfyd:export")
    @Log(title = "学生一分一段", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StudentYfyd studentYfyd) {
        studentYfyd.setScoreList(setSizes(studentYfyd.getScore()));
        studentYfyd.setPlaceList(setSizes(studentYfyd.getPlace()));
        List<StudentYfyd> noPageList = studentYfydService.selectStudentYfydListNoPage(studentYfyd);
        List<StudentYfyd> list = studentYfydService.selectStudentYfydLists(studentYfyd,noPageList);
        ExcelUtil<StudentYfyd> util = new ExcelUtil<StudentYfyd>(StudentYfyd.class);
        util.exportExcel(response, list, "学生一分一段数据");
    }

    /**
     * 获取学生一分一段详细信息
     */
    @RequiresPermissions("student:yfyd:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(studentYfydService.selectStudentYfydById(id));
    }

    /**
     * 新增学生一分一段
     */
    @RequiresPermissions("student:yfyd:add")
    @Log(title = "学生一分一段", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StudentYfyd studentYfyd) {
        return toAjax(studentYfydService.insertStudentYfyd(studentYfyd));
    }

    /**
     * 修改学生一分一段
     */
    @RequiresPermissions("student:yfyd:edit")
    @Log(title = "学生一分一段", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StudentYfyd studentYfyd) {
        return toAjax(studentYfydService.updateStudentYfyd(studentYfyd));
    }

    /**
     * 删除学生一分一段
     */
    @RequiresPermissions("student:yfyd:remove")
    @Log(title = "学生一分一段", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(studentYfydService.deleteStudentYfydByIds(ids));
    }

    /**
     * 导入学生一分一段excel
     */
    @Log(title = "学生一分一段", businessType = BusinessType.INSERT)
    @PostMapping("/excel")
    public AjaxResult addYfydExcel(@ApiParam("excel文件") @RequestParam("file") List<MultipartFile> files)
            throws IOException {
        studentYfydService.addExcel(files);
        return toAjax(true);
    }

}
